package com.niiwoo.civet.user.service.local.asset;


import com.niiwoo.civet.user.dao.entity.DataStatus;
import com.niiwoo.civet.user.dao.entity.IdentityInfo;
import com.niiwoo.civet.user.dao.entity.IdentityMapping;
import com.niiwoo.civet.user.dao.entity.LoginAccountMapping;
import com.niiwoo.civet.user.dao.mapper.DataStatusMapperExt;
import com.niiwoo.civet.user.dao.mapper.IdentityInfoMapperExt;
import com.niiwoo.civet.user.dao.mapper.IdentityMappingMapper;
import com.niiwoo.civet.user.dao.mapper.LoginAccountMappingMapperExt;
import com.niiwoo.civet.user.enums.UserDataStatusEnum;
import com.niiwoo.civet.user.enums.UserDataTypeEnum;
import com.niiwoo.tripod.base.component.PrivacyMasks;
import groovy.util.logging.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Description: 卡牛用户服务
 * @Author:LiJian
 * @Date:2018/2/13
 */
@Slf4j
@Service
public class CardniuUserService {

    @Autowired
    private LoginAccountMappingMapperExt loginAccountMappingMapper;

    @Autowired
    private IdentityMappingMapper identityMappingMapper;

    @Autowired
    private IdentityInfoMapperExt identityInfoMapper;

    @Autowired
    private DataStatusMapperExt dataStatusMapper;

    @Autowired
    private PrivacyMasks privacyMasks;

    /**
     * 根据手机号获取用户ID
     * @param mobileNo（不加密）
     * @return
     */
    public String getUserIdByMobileNo(String mobileNo) {
        if(StringUtils.isBlank(mobileNo)) {
            throw new IllegalArgumentException("入参为空！");
        }
        String userId = null;
        LoginAccountMapping loginAccountMapping = loginAccountMappingMapper.selectByPrimaryKey(privacyMasks.encryptPrivacy(mobileNo));
        if(loginAccountMapping != null) {
            userId = loginAccountMapping.getUserId();
        }
        return userId;
    }

    /**
     * 根据身份证号获取用户ID
     * @param idCard（不加密）
     * @return
     */
    public String getUserIdByIdCard(String idCard) {
        if(StringUtils.isBlank(idCard)) {
            throw new IllegalArgumentException("入参为空！");
        }
        String userId = null;
        IdentityMapping identityMapping = identityMappingMapper.selectByPrimaryKey(privacyMasks.encryptPrivacy(idCard));
        if(identityMapping != null) {
            userId = identityMapping.getUserId();
        }
        return userId;
    }

    /**
     * 身份信息比对
     * @param userId
     * @param name
     * @param idCard（不加密）
     * @return
     */
    public boolean checkIdentityInfo(String userId, String name, String idCard) {
        if(StringUtils.isNoneBlank(userId, name, idCard)) {
            throw new IllegalArgumentException("入参为空！");
        }
        String idCardMask = privacyMasks.encryptPrivacy(idCard);
        DataStatus dataStatus = dataStatusMapper.selectByUserIdAndType(userId, UserDataTypeEnum.TWO_ELEMENT.getValue());
        if(dataStatus != null && dataStatus.getDataStatus().equals(UserDataStatusEnum.RealNameAuthStatus.PASS_CERTIFIED.getStatus())) {
            IdentityInfo identityInfo = identityInfoMapper.selectByPrimaryKey(userId);
            if(identityInfo != null && (!name.equals(identityInfo.getName()) || !idCardMask.equals(identityInfo.getIdCard()))) {
                return false;
            }
        }else {
            IdentityMapping identityMapping = identityMappingMapper.selectByPrimaryKey(idCardMask);
            return identityMapping != null ? false : true;
        }
        return true;
    }



}
